package com.bdqn.zhang.dao.impl;

import com.bdqn.zhang.dao.VivoejDao;
import com.bdqn.zhang.dao.impl.dbutil.DButil;
import com.bdqn.zhang.pojo.Vivoej;
import com.bdqn.zhang.pojo.Vivoproduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VivoejDaoimpl extends DButil implements VivoejDao {
    Connection conn = null;
    PreparedStatement ment = null;
    ResultSet resultSet = null;

    @Override
    public int getCount() {
        conn = getConnerction();
        String sql = "select count(1) from Vivo_erji where shanchu=1";
        try {
            ment = conn.prepareStatement(sql);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivoej> getAllr() {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoej> list = new ArrayList();
        String sql = "select * from Vivo_erji where shanchu=1";
        try {
            ment = conn.prepareStatement(sql);
            //给参数传值
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoej vivoej = new Vivoej();
                    vivoej.setEid(resultSet.getInt(1));
                    vivoej.setYid(resultSet.getInt(2));
                    vivoej.setEtype(resultSet.getString(3));
                    vivoej.setResType(resultSet.getString(4));
                    vivoej.setFj(resultSet.getString(5));
                    vivoej.setImg(resultSet.getString(6));
                    vivoej.setZhuangtai(resultSet.getInt(7));
                    list.add(vivoej);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int del(String id) {
        String sql = "update Vivo_erji SET shanchu=0 where eid in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int delsc(String id) {
        String sql = "delete from Vivo_erji where eid in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int delhuifu(String id) {
        String sql = "update Vivo_erji SET shanchu=1 where eid in (?)";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int cz(String id) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        String sql = "select * from Vivo_erji where yid=(SELECT eid FROM Vivo_erji where eid=?) AND shanchu=1";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, id);
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                if (resultSet.next()) {
                    return resultSet.getInt(1);
                }
            }
            return 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public int delss(String id) {
        String sql = "DELETE from Vivo_erji WHERE eid=?";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    @Override
    public int edits(Vivoej vivoej) {
        String sql = "UPDATE Vivo_erji SET etype=?,img=? WHERE eid=?";
        Object[] obj = {vivoej.getEtype(), vivoej.getImg(), vivoej.getEid()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int add(Vivoej vivoej) {
        String sql = "INSERT INTO Vivo_erji(yid,etype,resType,fj,img,zhuangtai,shanchu) VALUES((select v.eid from Vivo_erji as v where v.etype=?),?,?,?,?,?,?)";
        Object[] obj = {vivoej.getFj(), vivoej.getResType(), vivoej.getEtype(), vivoej.getFj(), vivoej.getImg(), vivoej.getZhuangtai(),vivoej.getShanchu()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int updatyij(Vivoej vivoej) {
        String sql = "INSERT INTO Vivo_erji(etype,resType,fj,img,zhuangtai,shanchu) VALUES(?,?,?,?,?,?)";
        Object[] obj = {vivoej.getEtype(), vivoej.getResType(), vivoej.getFj(), vivoej.getImg(), vivoej.getZhuangtai(),vivoej.getShanchu()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int updatej(Vivoej vivoej) {
        String sql = "INSERT INTO Vivo_erji(yid,etype,resType,fj,img,zhuangtai,shanchu) VALUES((select v.eid from Vivo_erji as v where v.etype=?),?,?,?,?,?,?)";
        Object[] obj = {vivoej.getFj(), vivoej.getEtype(), vivoej.getResType(), vivoej.getFj(), vivoej.getImg(), vivoej.getZhuangtai(),vivoej.getShanchu()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int updatejsan(Vivoej vivoej) {
        String sql = "INSERT INTO Vivo_erji(yid,etype,resType,fj,img,zhuangtai,shanchu) VALUES((select v.eid from Vivo_erji as v where v.etype=?),?,?,?,?,?,?)";
        Object[] obj = {vivoej.getFj(), vivoej.getEtype(), vivoej.getResType(), vivoej.getFj(), vivoej.getImg(), vivoej.getZhuangtai(),vivoej.getShanchu()};
        return executeUpdate(sql, obj);
    }

    @Override
    public List<Vivoej> yjxg(String id) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoej> list = new ArrayList();
        String sql = "select etype from  Vivo_erji where eid=(select yid FROM Vivo_erji where etype=(select etype from  Vivo_erji where eid=(select yid FROM Vivo_erji where etype=?)))";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, id);
            //给参数传值
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoej vivoproduct = new Vivoej();
                    vivoproduct.setEtype(resultSet.getString(1));
                    list.add(vivoproduct);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }

    @Override
    public List<Vivoej> yjxgs(String id) {
        Connection conn = null;
        PreparedStatement ment = null;
        ResultSet resultSet = null;
        conn = getConnerction();
        List<Vivoej> list = new ArrayList();
        String sql = "select etype from  Vivo_erji where eid=(select yid FROM Vivo_erji where etype=?)";
        try {
            ment = conn.prepareStatement(sql);
            ment.setObject(1, id);
            //给参数传值
            resultSet = ment.executeQuery();
            if (resultSet != null) {
                while (resultSet.next()) {
                    Vivoej vivoproduct = new Vivoej();
                    vivoproduct.setEtype(resultSet.getString(1));
                    list.add(vivoproduct);
                }
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(conn, ment, resultSet);
        }
    }
//    @Override
//    public int updatejsans(Vivoej vivoej) {
//        String sql="INSERT INTO Vivo_product(yid)VALUES((select v.eid from Vivo_erji as v where v.etype=?))";
//        Object[] obj={vivoej.getFj()};
//        return executeUpdate(sql,obj);
//    }
}
